/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RTS.net;

import java.io.*;
import java.net.*;

public class KnockKnockClient {
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        String host = "";
        int port = 4444;
        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Hostname: ");
        host = stdIn.readLine();
        // 129.31.72.189
        byte [] b = new byte[] {(byte)172,(byte)22,(byte)12,(byte)41};
        InetAddress serverIP = null;
        try {
            serverIP = InetAddress.getByAddress(b);
        } catch (UnknownHostException impossible) {
            System.err.println("impossible.");
        }
        System.out.print("Port: ");
        port = Integer.parseInt(stdIn.readLine());

        try {
            kkSocket = new Socket(serverIP, port);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: "+host);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to:"+host);
            System.exit(1);
        }

        String fromServer;
        String fromUser;

        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server says: " + fromServer);
            if (fromServer.equals("Bye."))
                break;
            System.out.print("Client says: ");
            fromUser = stdIn.readLine();
	    if (fromUser != null) {
                out.println(fromUser);
	    }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }
}